package com.aivin.leakcanarysample.tool

import android.os.Debug
import shark.*
import java.io.File
import java.util.*

class SharkTool {

    public fun watchActivity( ) :String {
        WkLog.showMsg("开始保存 .hprof 文件")
        val heapDumpFile = File(MyFileTool.getLeakWatchDir() + "/" + UUID.randomUUID().toString() + ".hprof")
        //dump 一份堆内存快照
        Debug.dumpHprofData(heapDumpFile.absolutePath)
        WkLog.showMsg(".hprof 文件保存完毕. "+ heapDumpFile.absolutePath)

        val heapAnalyzer = HeapAnalyzer(OnAnalysisProgressListener.NO_OP)
        WkLog.showMsg("开始分析 .hprof 文件")
        //分析堆内存快照
        val analysis = heapAnalyzer.analyze(
                heapDumpFile = heapDumpFile,
                leakingObjectFinder = KeyedWeakReferenceFinder,
                referenceMatchers = AndroidReferenceMatchers.appDefaults,
                computeRetainedHeapSize = true,
                objectInspectors = AndroidObjectInspectors.appDefaults,
                metadataExtractor = AndroidMetadataExtractor
        )

        return analysis.toString()

    }

}


